package com.vainycos.lotterysearch.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vainycos.lotterysearch.mapper.WorldCupMapper;
import com.vainycos.lotterysearch.model.WorldCupDO;
import com.vainycos.lotterysearch.model.worldcup.CompetitionData;
import com.vainycos.lotterysearch.model.worldcup.Worldcup;
import com.vainycos.lotterysearch.utils.FreeApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/21 17:07
 */
@Service
public class WorldcupService extends ServiceImpl<WorldCupMapper, WorldCupDO> {

    private static List<String> times;

    @Autowired
    private WorldCupMapper worldCupMapper;

    static {
        times = Arrays.asList("11-20", "11-21", "11-22", "11-23", "11-24", "11-25", "11-26", "11-27",
                "11-28", "11-29", "11-30", "12-01", "12-02", "12-03", "12-04", "12-05",
                "12-06", "12-08", "12-09", "12-10", "12-13", "12-14", "12-16", "12-17");
    }

    /**
     * 初始化概览比赛数据（先清空，再进行全量更新）
     */
    public boolean initOverview(){
        // 待初始化新增数据
        List<WorldCupDO> worldcups = new ArrayList<>();
        worldCupMapper.truncate();
        List<Worldcup> allWorldcupInfo = FreeApiUtil.getAllWorldcupInfo();
        allWorldcupInfo.forEach(worldcup->{
            List<String> startTimes = new ArrayList<>();
            List<String> matchNames = new ArrayList<>();
            CompetitionData competitionData = worldcup.getData().get(0);
            WorldCupDO worldCupDO = new WorldCupDO();
            worldCupDO.setTime(competitionData.getTime());
            competitionData.getList().forEach(c->{
                startTimes.add(c.getStartTime());
                matchNames.add(c.getMatchName());
            });
            worldCupDO.setStartTime(CollectionUtil.join(startTimes, ","));
            worldCupDO.setMatchName(CollectionUtil.join(matchNames, ","));
            worldCupDO.setData(JSONObject.toJSONString(worldcup));
            worldcups.add(worldCupDO);
        });
        return saveBatch(worldcups);
    }


}
